package com.xxxcompany.shop.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xxxcompany.shop.dao.IUserDAO;
import com.xxxcompany.shop.dao.impl.UserDAOImpl;
import com.xxxcompany.shop.entity.User;
import com.xxxcompany.shop.service.IUserService;
import com.xxxcompany.shop.service.impl.UserServiceImpl;

// 1. 新建一个类,继承HttpServlet

// 3. 配置Servlet的映射路径
@WebServlet("/registerchk")
public class RegisterCheckServlet extends HttpServlet {

	IUserService userService = new UserServiceImpl();

	// 2. 重写doGet方法处理Get请求
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/html;charset=utf-8");
		// HttpServletRequest req 请求对象
		// .getParameter() 得到用户发来的请求参数信息
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		System.out.println(username + "," + password);
		// HttpServletResponse resp 响应对象
		// 通过它给客户端发送西悉尼

		// 得到一个字符输出流
		PrintWriter pw = resp.getWriter();
		if (username.equals("") || password.equals("")) {
			pw.write("账号或者密码不能为空！");
		} else {
			if (userService.insert(username, password) == 1) {
				pw.write("注册成功");
			} else {
				pw.write("注册失败，用户名重复");
			}
		}

	}
}
